package com.java.controller;

import com.java.model.*;
import com.java.service.IndexService;
import com.java.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class IndexController {
    @Autowired
    private IndexService indexService;

    //访问主页
    @Autowired
    private MediaService mediaService;

    @RequestMapping("/index")
    public String index(Model model,Media media) {
        List<Media> list = mediaService.list(media);
        List<Media> listVideo = mediaService.listVideo(media);
        model.addAttribute("list",list);
        model.addAttribute("listVideo",listVideo);
        return "/home";
    }


    //发送短信验证码
    @RequestMapping("/massage")
    @ResponseBody
    public Result massage(HttpServletRequest request,String phoneNum,Account account) {
        return  indexService.register(phoneNum,request,account);
    }
    //校验验证码是否正确
    @RequestMapping("/CheckCode")
    @ResponseBody
    public Result CheckCode(String phoneNum,String verification,String password,String username) {
        Account account = new Account();
        account.setUsername(username);
        account.setPhoneNum(phoneNum);
        account.setPassword(password);
        account.setVerification(verification);
        return indexService.CheckCode(account);
    }
//登录
    @RequestMapping("/openLogin")
    public String toLogin() {
        return "/login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(HttpServletRequest request,String phone, String password3, Model model){
        Account account = new Account();
        account.setPhoneNum(phone);
        account.setPassword(password3);
        return indexService.login(account,request,model);
    }
    //忘记密码
    @RequestMapping("/openForget")
    public String openForget(){
        return "forget";
    }

    //发送短信验证码
    @RequestMapping("/fogetMassage")
    @ResponseBody
    public Result fogetMassage(HttpServletRequest request,String phoneNum,Account account) {
        return  indexService.forget(phoneNum,request,account);
    }
    //发送短信验证码
    @RequestMapping("/fogetMassage2")
    @ResponseBody
    public Result fogetMassage2(HttpServletRequest request,String phoneNum,Account account) {
        return  indexService.forgetMedia(phoneNum,request,account);
    }
    @RequestMapping("/forget")
    @ResponseBody
    public Result forget(String phoneNum,String verification,String password){
        Account account = new Account();
        account.setPhoneNum(phoneNum);
        account.setPassword(password);
        account.setVerification(verification);
        return indexService.CheckCode2(account);
    }

    //修改密码
    @RequestMapping("/updatePwd")
    @ResponseBody
    public Result updatePwd(Account account){
        return indexService.updatePwd(account);
    }

    //个人中心
    @RequestMapping("/Personal")
    public String Personal(int id,Model model,HttpServletRequest request) {
        Account personal = indexService.Personal(id, request, model);
        model.addAttribute("personal",personal);
        return "/Personal";
    }

    //编辑个人信息
    @RequestMapping("/edit")
    public String edit(int id,Model model) {
        Result result = indexService.get(id);
        if (result.getCode().equals("0")) {
            model.addAttribute("account", result.getData());
        }
        return "/edit";
    }

    @RequestMapping("/modify")
    @ResponseBody
    public Result modify(@RequestBody Account account,Model model) {
        model.addAttribute("account",account);
        return indexService.modify(account);
    }

    //查询余额
    @RequestMapping("/balance")
    public String balance(int id,Model model,HttpServletRequest request){
        Advertiser balance = indexService.balance(id, request, model);
        model.addAttribute("balance",balance);
        return "balance";
    }
    //充值余额
    @RequestMapping("/recharge")
    @ResponseBody
    public Result recharge(int id,Double amount){
//        Finance finance = new Finance();
//        finance.setId(id);
//        finance.setBalance(balance);
        return indexService.recharge(id,amount);
    }
    //访问自媒体
    @RequestMapping("/media")
    public String media(int platformId, Model model, Media media){
        media.setPlatformId(platformId);
        model.addAttribute("platformId",platformId);
        List<Media> listTiktok = mediaService.listTiktok(media);
        model.addAttribute("listTiktok",listTiktok);
        model.addAttribute("media",media);
        return "/tiktok";
    }
    //访问自媒体
    @RequestMapping("/search")
    public String search(Model model, Media media){
        List<Media> listTiktok2 = mediaService.listTiktok2(media);
        model.addAttribute("listTiktok",listTiktok2);
        model.addAttribute("media",media);
        return "/tiktok2";
    }
    //访问账号详情
    @RequestMapping("/particulars")
    public String particulars(int id,Model model,Media media){
        media.setId(id);
        Media particulars = mediaService.particulars(media);
        model.addAttribute("particulars",particulars);
        return "/particulars";
    }
    //按粉丝数量访问
    @RequestMapping("/media2")
    public String media2(int platformId, Model model, Media media){
        media.setPlatformId(platformId);
        List<Media> listFans = mediaService.listFans(media);
        model.addAttribute("listFans",listFans);
        model.addAttribute("media",media);
        return "/fans";
    }

    //按报价访问
    @RequestMapping("/media3")
    public String media3(int platformId, Model model, Media media){
        media.setPlatformId(platformId);
        List<Media> listQuote = mediaService.listQuote(media);
        model.addAttribute("listQuote",listQuote);
        model.addAttribute("media",media);
        return "/quote";
    }

    //按阅读量访问
    @RequestMapping("/media4")
    public String media4(int platformId, Model model, Media media){
        media.setPlatformId(platformId);
        List<Media> listReading = mediaService.listReading(media);
        model.addAttribute("listReading",listReading);
        model.addAttribute("media",media);
        return "/reading";
    }
    //立即投放
    @RequestMapping("/openApply")
    public String openApply(int mediaId,int price,int adsId,Model model){
        Classify classify = new Classify();
        List<Classify> classifies = indexService.listApply(classify);
        model.addAttribute("classifies",classifies);
        model.addAttribute("id",mediaId);
        model.addAttribute("price",price);
        model.addAttribute("adsId",adsId);
        return "/apply";
    }

    @RequestMapping("/apply")
    @ResponseBody
    public Result apply(Apply apply){
        return indexService.apply(apply);
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request, Model model) {
        // 获取上传文件的名称
        String filename = multipartFile.getOriginalFilename();
        // 生成UUID
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        // 获取上传目录
        String realPath = request.getSession().getServletContext().getRealPath(request.getContextPath()) + "/image";
        // 上传对象
        File uploadFile = new File(realPath, uuid + filename);
        try {
            // 上传文件
            multipartFile.transferTo(uploadFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String str = "";
        str = "/image/"+uuid+filename;
        model.addAttribute("proof",str);
        return Result.success(str,  null) ;
    }
    @RequestMapping("edit2")
    public String edit(){
        return "/user/edit";
    }

    @RequestMapping("advertising")
    public String advertising(){
        return "/will";
    }

}

